package ch.zbw.swt.sysventory.network.scan;

import ch.zbw.swt.sysventory.hibernate.entity.Computer;
import ch.zbw.swt.sysventory.hibernate.entity.Processor;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class ScanProcessor {

	// Init data Typs

	public String nameProcessor;
	public String familyProcessor;
	public int coresProcessor;
	public int clockProcessor;

	private Processor cpu;
	private Computer computer;

	// Constructor
	public ScanProcessor(Computer computer) {
		this.computer = computer;
	}

	/*
	 * Read entrys from Processor
	 */
	public Processor getProcessor(String host) {
		String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
		String query = "SELECT * FROM Win32_Processor";

		try {
			ActiveXComponent axWMI = new ActiveXComponent(connectStr);
			// Execute the query
			Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));

			// The Result is a Collection
			EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
			Dispatch item = null;
			while (enumVariant.hasMoreElements()) {
				item = enumVariant.nextElement().toDispatch();

				String nameProcessor = Dispatch.call(item, "Name").toString();
				String familyProcessor = Dispatch.call(item, "Family")
						.toString();
				String cores = Dispatch.call(item, "NumberOfCores").toString();
				String clock = Dispatch.call(item, "MaxClockSpeed").toString();
				int coresProcessor = Integer.valueOf(cores).intValue();
				int clockProcessor = Integer.valueOf(clock).intValue();

				cpu = new Processor(nameProcessor, familyProcessor,
						coresProcessor, clockProcessor, computer);

			}
		} catch (Exception e) {

		}

		return cpu;

	}

}
